
package com.kweative.model.column;

import com.kweative.model.Table;
import com.kweative.model.sql.SQLDataType;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author henrik
 */
public class ColumnText<T extends Table> extends Column<ColumnText,T> {
    private int size = -1;
    public ColumnText(String name, Size size) {
        this(size);
		setName(name);
    }

    public ColumnText(String name, int size) {
        this(size);
		setName(name);
    }
	public ColumnText(int size) {
        super(String.class);
        setSize(size);
    }
	public ColumnText(Size size) {
        super(String.class);
        setSize(size);
    }
	public void setSize(Size size) {
		switch(size) {
            case TITLE:
				setSize(65);
                break;
            case NAME:
				setSize(120);
                break;
            case TEASER:
				setSize(255);
                break;
            case BODY:
				setSize(-1);
                break;
        }
	}
	public void setSize(int size) {
		this.size = size;
		if (this.size < 0) {
			setSQLDataType(SQLDataType.TEXT);
		} else {
			setSQLDataType(SQLDataType.VARCHAR);
			setSQLTypeParms(String.valueOf(this.size));
		}
	}

    @Override
    public String serialize(Object value) {
        if (value == null) return null;
        String str = (String) value;
        str = StringUtils.replace(str,"'","\\'").trim();
        str = String.format("'%s'",str);
        return str;
    }


    public enum Size {
        TITLE,
        TEASER,
        BODY,
        NAME
    }

}
